package com.suning.sawp.service.impl.dao.task;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.mainpush.TaskCommStatistic;
import com.suning.sawp.dto.mainpush.TaskGdsStatistic;
import com.suning.sawp.dto.mainpush.TaskNoDto;
import com.suning.sawp.dto.mainpush.TaskStoreTempDto;
import com.suning.sawp.dto.task.StoreManTask;
import com.suning.sawp.intf.dao.task.TaskStoreAndClerkDaoService;
import com.suning.sawp.po.mainpush.MainPushTask;
import com.suning.sawp.po.task.TaskClerk;

@Service("taskStoreAndClerkDaoService")
public class TaskStoreAndClerkDaoServiceImpl implements TaskStoreAndClerkDaoService {

    @Resource
    DalClient dalClient;
    
    @Override
    public List<StoreManTask> queryProcessingStoreTasks(String storeCode, Set<String> categoryCodes) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        if (CollectionUtils.isNotEmpty(categoryCodes)) {
            paramMap.put("categoryCodes", categoryCodes);
        }
        Set<String> taskStatuses = new HashSet<String>();
        taskStatuses.add(MainPushTask.TaskStatusConstants.NOT_BEGIN);
        taskStatuses.add(MainPushTask.TaskStatusConstants.CARRIED_OUT);
        taskStatuses.add(MainPushTask.TaskStatusConstants.IS_DELETE);
        paramMap.put("taskStatus", taskStatuses);
        List<StoreManTask> tasks = dalClient.queryForList(
                "STORE_MAIN_PUSH.QUERY_PROCESS_STORE_TASK", paramMap, StoreManTask.class);
        
        // 更改门店任务-任务状态为已删除未阅改为已删除已阅
        paramMap.put("taskStatus", MainPushTask.TaskStatusConstants.IS_DELETE);
        paramMap.put("deleteRead", MainPushTask.TaskStatusConstants.DEL_READ);
        dalClient.execute("STORE_MAIN_PUSH.UPDATE_STORE_TASK_DEL_READ", paramMap);
        return tasks;
    }

    @Override
    public List<TaskClerk> queryProcessingStaffTasks(String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        Set<String> taskStatus = new HashSet<String>();
        taskStatus.add(MainPushTask.TaskStatusConstants.CARRIED_OUT);
        taskStatus.add(MainPushTask.TaskStatusConstants.IS_DELETE);
        paramMap.put("taskStatus", taskStatus);
        List<TaskClerk> tasks = dalClient.queryForList(
                "STORE_MAIN_PUSH.QUERY_PROCESS_STAFF_TASK", paramMap, TaskClerk.class);
        
        // 变更删除未阅的店员任务为删除已阅
        paramMap.put("taskStatus", MainPushTask.TaskStatusConstants.IS_DELETE);
        paramMap.put("deleteRead", MainPushTask.TaskStatusConstants.DEL_READ);
        dalClient.execute("STORE_MAIN_PUSH.UPDATE_CLERK_TASK_DEL_READ", paramMap);
        return tasks;
    }

    @Override
    public List<TaskNoDto> queryHqTaskNosByClerk(Set<Long> taskNos) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNos", taskNos);
        List<TaskNoDto> hqTaskNos = dalClient.queryForList(
                "STORE_MAIN_PUSH.QUERY_HQ_TASK_NO_BY_CLERK", paramMap, TaskNoDto.class);
        return hqTaskNos;
    }

    @Override
    public List<StoreManTask> queryBiClerkMainpushTasks(String staffId, String storeCode, List<Long> taskNos) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("taskNos", taskNos);
        List<StoreManTask> tasks = dalClient.queryForList(
                "STORE_MAIN_PUSH.QUERY_BI_CLERK_MAINPUSH_TASK", paramMap, StoreManTask.class);
        return tasks;
    }

    @Override
    public TaskCommStatistic getGoodsCodesInTaskClerk(Long taskNo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        Set<String> delCommStatus = new HashSet<String>();
        delCommStatus.add(MainPushTask.CommodityStatusConstants.DEL_READ);
        delCommStatus.add(MainPushTask.CommodityStatusConstants.DEL_NO_READ);
        paramMap.put("delCommStatus", delCommStatus);
        List<TaskGdsStatistic> goodsCodes = dalClient.queryForList(
                "STORE_MAIN_PUSH.QUERY_CLERK_TASK_GDS", paramMap, TaskGdsStatistic.class);
        Set<String> gdsCds = new HashSet<String>();
        int sum = 0;
        for (TaskGdsStatistic statistic : goodsCodes) {
            String[] args = statistic.getGoodsCode().split(",");
            for (String gdsCode : args) {
                gdsCds.add(gdsCode);
            }
            sum += statistic.getDestNum();
        }
        TaskCommStatistic statistic = new TaskCommStatistic();
        statistic.setGoodsCode(gdsCds);
        statistic.setSum(sum);
        statistic.setTaskNo(taskNo);
        
        return statistic;
    }

    @Override
    public Long queryHqTaskNo(Long taskNo, String orgLevel) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        Long hqTaskNo = null;
        if (MainPushTask.OrgLevelConstants.LEVEL_STORE.equals(orgLevel)) {
            // 门店任务
            hqTaskNo = dalClient.queryForObject("STORE_MAIN_PUSH.QUERY_HQ_TASK_NO_STORE", paramMap, Long.class); 
        } else if (MainPushTask.OrgLevelConstants.LEVEL_CLERK.equals(orgLevel)) {
            // 店员任务
            hqTaskNo = dalClient.queryForObject("STORE_MAIN_PUSH.QUERY_HQ_TASK_NO_CLERK", paramMap, Long.class); 
        }
        return hqTaskNo;
    }

    @Override
    public TaskStoreTempDto queryTaskStoreByTaskNo(Long taskNo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        return dalClient.queryForObject("STORE_MAIN_PUSH.QUERY_STORE_TASK", paramMap, TaskStoreTempDto.class);
    }

    @Override
    public TaskClerk queryClerkTask(String staffId, String categoryCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("categoryCode", categoryCode);
        paramMap.put("taskStatus", MainPushTask.TaskStatusConstants.CARRIED_OUT);
        return dalClient.queryForObject("STORE_MAIN_PUSH.QUERY_CLERK_TASK_STAFF_ID_CATE_CODE", paramMap, TaskClerk.class);
    }

    @Override
    public TaskClerk queryClerkTaskByTaskNo(Long taskNo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        return dalClient.queryForObject("STORE_MAIN_PUSH.QUERY_CLERK_TASK_BY_TASK_NO", paramMap, TaskClerk.class);
    }

    @Override
    public List<String> queryCommCdsInRegion(String regionCode, String cateCode, Date startTime, Date endTime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("regionCode", regionCode);
        paramMap.put("cateCode", cateCode);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        paramMap.put("taskStatus", MainPushTask.TaskStatusConstants.CARRIED_OUT);
        
        return dalClient.queryForList("STORE_MAIN_PUSH.QUERY_REGION_MAIN_PUSH_COMM_CODES", paramMap, String.class);
    }

}
